<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/3
 * Time: 12:21
 */

namespace Admin\Controller;
use Admin\Controller\AdminController;
use Admin\Model\ItemModel;
use Admin\Model\DonationModel;
class DonationController extends AdminController
{
    //查询所有捐款项目
    public function itemList ()
    {
        $this->checkPower();
        $item_name = "";
        if(!empty($_GET['item'])){
            $item_name= I("get.item");
        }
        $status = "";
        if(!empty($_GET['status'])){
            $status = I("get.status");
        }
        $data['item'] = $item_name;
        $data['status'] = $status;
        $item = new ItemModel();
        $res = $item->queryAllItem($data);
        $list = $res['list'];
        $show = $res['show'];
        $this->title = "捐款项目表";
        $this->itemList = $list;
        $this->page = $show;
        $this->display("Donation/itemList");
    }

    //添加新项目$this->checkPower();
    public function addItem ()
    {
        $this->checkPower();
        $this->title = "添加新项目";
        $this->assign("prev",$_SERVER['HTTP_REFERER']);
        $this->display("Donation/addItem");
    }

    //保存新项目
    public function saveItem ()
    {
        $introduce = I('post.introduce');
        if(empty($introduce)){
            $this->error("详情介绍不能为空");
        }
        $item_name = I('post.item');
        $end_time = date("Y-m-d H:i:s",strtotime(I("post.end_time")));
        $create_time = date("Y-m-d H:i:s");
        if(strtotime($end_time) < strtotime($create_time)){
            $this->error("截止日期不能小于当前日期");
        }
        $data["item"] = $item_name;
        $data["end_time"] = $end_time;
        $data["create_time"] = $create_time;
        $data["introduce"] = $introduce;

        $item = new ItemModel();
        $res = $item->saveItem($data);
        if($res > 0){
            $this->redirect("Donation/itemList");
        }else{
            $this->error("新增失败");
        }
    }

    //查询所有捐款记录$this->checkPower();
    public function index ()
    {
        $this->checkPower();
        //根据捐款人名搜索
        $name = "";
        if(!empty($_GET['name'])){
            $name = I("get.name");
        }
        //根据捐款项目
        $item = "";
        if(!empty($_GET['item'])){
            $item = I("get.item");
//            echo $sort;die;
        }
        //根据捐款时间搜索
        $time= "";
        if(!empty($_GET['time'])){
            $time = I("get.time");
        }

        $data['name'] = $name;
        $data['item'] = $item;
        $data['time'] = $time;

        //排序字段
        $order = "";
        if(!empty($_GET['order'])){
            $order = I("get.order");
        }
        //排序方式
        $order1 = "";
        if(!empty($_GET['order1'])){
            $order1 = I("get.order1");
        }
        $data['order'] = $order;
        $data['order1'] = $order1;

        $this->title = "捐款记录";
        $don = new DonationModel();
        $res = $don -> queryAllDon($data);
        $this->list = $res['list'];
        $this->page = $res['show'];
        //查询总金额
        $totalPrice = $don -> queryAllPrice($data);
        $this->totalPrice = $totalPrice['totalprice'];//$totalPrice

        //查看所有捐款项目
        $item = new ItemModel();
        $itemList = $item -> queryAllItems();
        $this->itemList = $itemList;
        $this->display("Donation/index");
    }

//    //根据项目id 查询捐款详情
//    public function showDetail()
//    {
//        $id = I('get.id');
//        $this->title = "捐款详情";
//        $don = new DonationModel();
//        $res = $don -> queryAllListByItem($id);
//        $this->list = $res['list'];
//        $this->page = $res['show'];
//        $this->display("Donation/detail");
//    }
//
//    //按年份查询捐款记录
//    public function queryAllListByYear()
//    {
//        $this->title = "按年份查询捐款记录";
//        $don = new DonationModel();
//        $list = $don -> queryAllPriceByYear();
//        $this->list = $list;
//        //查询总金额
//        $totalPrice = $don -> queryAllPrice();
////        echo $totalPrice['totalPrice'];die;
//        $this->totalPrice = $totalPrice['totalprice'];//$totalPrice
//        $this->display("Donation/itemByYear");
//    }
//
//    //查看每年的详细捐款记录
//    public function showYearDetail ()
//    {
//        $year = I('get.year');
////        echo $year;die;
//        $this->title = $year."年捐款详情";
//        $don = new DonationModel();
//        $res = $don -> queryAllListByYear($year);
//        $this->list = $res['list'];
//        $this->page = $res['show'];
//        $this->display("Donation/yearDetail");
//    }
//
    //查看捐款项目详情根据项目id
    public function showItemDetail()
    {
        $id = I('get.id');
        $item = new ItemModel();
        $info = $item->queryItemDetailById($id);
        $this->title = "捐款项目详情";
        $this->itemInfo = $info;
        $this->assign("prev",$_SERVER['HTTP_REFERER']);
        $this->display("Donation/itemDetail");
    }
}